<?php
namespace app\common\model\quotation;
use think\Model;
use app\common\model\orderdata\Goods;

class Quotation extends Model
{
     // 设置当前模型对应的完整数据表名称
        protected $table = '';
        protected $pk = 'QuotationId';
        protected $type = '';

    /**
     * 构造函数 复用此模型
     * 用type判断是否存在此表
     * @param [type] $type [description]
     * @param [type] $data [description]
     */
    public function __construct($data=[]){
        if(empty(input('Type'))){
            moerr();
        }else{
            $type = input('Type');
        }
    	$tblist = ['hawa','buss'];
    	$namelist  = ['company','bussiness'];
    	foreach ($tblist as $key => $value) {
    		if($type==$value){
    			$this->table = 'quotation_'.$namelist[$key];
    			$this->type = $type;
    			break;
    		}
    	}
    	if(empty($this->table)){
            moerr('没有找到'.$type.'系统');
    	}
    	parent::__construct($data);
    }
    public function goodInfo()
    {
        return $this->hasOne('app\common\model\orderdata\Goods','GoodId','GoodId');
    }

    /**
     * 格式化列表
     * 重新装载为前端使用样式
     * @param  [type] $QuotationListId [description]
     * @return [type]                  [description]
     */
    public static function getlist($QuotationListId){
        $data = Quotation::where('QuotationListId',$QuotationListId)->with('goodInfo')->select();
        $data = $data->toArray();
        $ndata = [];
        foreach ($data as $key => $value) {
            if(!empty($value['GoodId'])){
                $ndata[]=['GoodId'=>$value['GoodId'],
                      'value' =>$value['Price'],
                      'Features'=>$value['good_info']['Features'],
                      'IndustrySource'=>$value['good_info']['IndustrySource'],
                      'Name'=>$value['good_info']['Name'],
                      'OrderNumber'=>$value['good_info']['OrderNumber'],
                      'Type'=>$value['good_info']['Type']];
            }
        }
        return $ndata;
    }
    /**
     * 获取询单表中的产品报价及产品id
     * @param  [type] $QuotationListId [description]
     * @param  [type] $goodlist        [description]
     * @return [type]                  [description]
     */
    public static function getquestionlist($QuotationListId,$goodlist){
        $data = Quotation::where('QuotationListId',$QuotationListId)
                              ->where('GoodId','in',$goodlist)
                              ->select();
        $ndata = [];
        return $data->visible(['GoodId','Price'])->toArray();
    }
    /**
     * 新增方法
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function screate($data){
        if(empty($data['QuotationListId'])){
            moerr('缺少报价表核心参数');
        }
        if(count($data['GoodId'])!==count($data['value'])){
            moerr('报价参数不匹配');
        }
        $cdata = [];
        $ndata = [];
        $ndata['QuotationListId'] = $data['QuotationListId'];
    	foreach ($data['GoodId'] as $key => $value) {
            $ndata['GoodId'] = $value;
            $ndata['Price'] = $data['value'][$key];
            $cdata[] = $ndata;
        }
        $qu = new Quotation($this->type);
        $data = $qu->saveAll($cdata);
    	return $data;
    }
}
